/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2009-2020 Rock Mao
* creator : Rock Mao
* created : 2009-5-2   22:29
* filename: LaborD3D10Texture.cpp
-----------------------------------------------------------------------------
*/
#include "LaborD3D10Texture.h"
#include "LaborD3D10RenderSystem.h"
#include "LaborD3D10Helps.h"
NAMESPACE_LABOR3D_BEGIN
////---------------------------------------------------------------------
//CD3D10TextureWrapper::CD3D10TextureWrapper()
//{
//	m_bLockable  = true;
//	m_pD3DTexture = NULL;
//	m_pDevice  = NULL;
//	m_initValue = 0;
//	m_nComponent    = 3;
/////*	m_PixelFmt = D3DFMT_*/A8R8G8B8;
//	setTextureSize(0,0,0,0);
//	m_nComponent = 0;
//	m_nByteAlign = NBYTE_ALAIGN;
//}
//
////---------------------------------------------------------------------
//float CD3D10TextureWrapper::getMax_U()
//{
//	return m_Width/(float)m_TexWidth;
//}
//
////---------------------------------------------------------------------
//float CD3D10TextureWrapper::getMax_V()
//{
//	return m_Height/(float)m_TexHeight;
//}
//
//
////---------------------------------------------------------------------
//void CD3D10TextureWrapper::setTextureSize(int w , int h , int texW , int texH)
//{
//	m_Width = w;
//	m_Height = h;
//	m_TexWidth = texW;
//	m_TexHeight = texH;
//}
//
//void CD3D10TextureWrapper::__destoryD3DTexture(CLD3D10RenderSystem* pRenderer)
//{
//	if(m_pD3DTexture)
//	{
//		pRenderer->freeMem(m_pD3DTexture);
//		m_pD3DTexture->Release();
//	}
//
//	m_pD3DTexture = NULL;
//}
//void CD3D10TextureWrapper::__unload(CLD3D10RenderSystem* pRenderer)
//{
//	m_pDevice  = NULL;
//	m_initValue = 0;
//	m_nComponent    = 3;
//	//m_PixelFmt = D3DFMT_A8R8G8B8;;
//	m_Width = 0;
//	m_Height = 0;
//	m_nComponent = 0;
//	m_nByteAlign = NBYTE_ALAIGN;
//	__destoryD3DTexture(pRenderer);
//}


//ID3D10ShaderResourceView* CD3D10TextureWrapper::__createTexture(int w , int h, bool bRenderTarget,CLD3D10RenderSystem* pRenderer)
//{
//	//HRESULT hr = S_OK;
//	//if(w == 0 ) w = 1;
//	//if(h == 0)  h = 1;
//	//ID3D10ShaderResourceView* pTexture = NULL;
//	//D3DX10_IMAGE_LOAD_INFO pLoadInfo;
//	//pLoadInfo.Height = h;
//	//pLoadInfo.Width  = w;
//	//if(bRenderTarget) 
//	//{
//	//	m_bLockable = false;
//	//	hr = D3DX10CreateShaderResourceViewFromFile( m_pDevice->get(), L"seafloor.dds", pLoadInfo, NULL, &g_pTextureRV, NULL );
//
//	//	if(FAILED(hr))
//	//	{
//	//		return NULL;
//	//	}
//	//}
//	//else
//	//{
//	//	m_bLockable = true;
//	//	if(SUCCEEDED(m_pDevice->get()->CreateTexture(w,h,1, 0 ,m_PixelFmt,D3DPOOL_MANAGED,&pTexture,0)) )
//	//	{
//	//		pRenderer->useMem(pTexture);
//	//		return pTexture;
//	//	}
//
//	//	m_bLockable = false;
//	//	if(SUCCEEDED(m_pDevice->get()->CreateTexture(w,h,1, 0 ,m_PixelFmt,D3DPOOL_DEFAULT,&pTexture,0)) )
//	//	{
//	//		pRenderer->useMem(pTexture);
//	//		return pTexture;
//	//	}
//	//	return NULL;
//
//	//}
//	//pRenderer->useMem(pTexture);
//	//return pTexture;
//
//	return NULL;
//}

//void CD3D10TextureWrapper::__InitTexture(bool bRenderTarget,CLD3D10RenderSystem* pRenderer)
//{
//	__destoryD3DTexture(pRenderer);
//
//	int w = m_TexWidth;
//	int h = m_TexHeight;
//	if(w == 0 || h == 0) return ;
//	m_pD3DTexture = __createTexture(w , h , bRenderTarget ,pRenderer);
//	if(m_pD3DTexture == NULL)
//		return ;
//	if( !bRenderTarget)
//	{
//		int _alginWidth = m_Pitch;
//		char* pbuf = new char [_alginWidth * h ];
//		memset(pbuf, m_initValue , _alginWidth * h ) ;
//		__uploadTexture(pbuf);
//		delete [] pbuf;
//	}
//}
//
//bool CD3D10TextureWrapper::__uploadUnlockableTexture(const char* pData)
//{
//	//if not support NPOT(none-power of 2), we should enlarge the texture size to POT.
//	//We should use this POT texture size to create texture. not the buffer size(NPOT)
//
//
//	//int w = m_TexWidth;
//	//int h = m_TexHeight;
//	//const char* pPixel = pData + m_Offset;
//	//if(m_pLockableSurf == NULL)
//	//{
//	//	D3DFORMAT d3dFmt = m_PixelFmt;
//	//	if(m_PixelFmt == D3DFMT_A8 || m_PixelFmt == D3DFMT_L8)
//	//		d3dFmt = D3DFMT_P8;
//	//	if(FAILED(m_pDevice->dev()->CreateOffscreenPlainSurface(w,h,d3dFmt,D3DPOOL_SYSTEMMEM,&m_pLockableSurf,0)) )
//	//	{
//	//		return false;
//	//	}
//	//}
//	//bool bRet = __uploadSurfaceData(m_pLockableSurf,pPixel);
//	////Now update Texture Rect;
//	//IDirect3DSurface9* pSurface;
//	//m_pD3DTexture->GetSurfaceLevel(0,&pSurface);
//	//HRESULT hr = m_pDevice->get()->UpdateSurface(m_pLockableSurf , NULL , pSurface , NULL);
//	//pSurface->Release();
//	//if(FAILED(hr)) return false;
//	return true;
//}
//
//bool CD3D10TextureWrapper::__uploadTexture(const char* pData)
//{
//	//if(pData == NULL)
//	//	return false;
//
//	//if(m_bLockable == false)
//	//	return __uploadUnlockableTexture(pData);
//	//const char* pPixel = pData + m_Offset;
//
//	//IDirect3DSurface9* pSurface;
//	//m_pD3DTexture->GetSurfaceLevel(0,&pSurface);
//	//bool bRet = __uploadSurfaceData(pSurface,pPixel);
//	//pSurface->Release();
//	//return bRet;
//
//	return true;
//}

CLD3D10BasicTexture::CLD3D10BasicTexture(CLD3D10RenderSystem* pDevice)
{
	m_pRenderer = pDevice;
}

CLD3D10BasicTexture::~CLD3D10BasicTexture()
{

}

int CLD3D10BasicTexture::Release()	
{
	m_refCount --;  
	int refCount = m_refCount;   
	if(m_refCount == 0) 
		delete this; 
	return refCount; 
}

int  CLD3D10BasicTexture::KillObject()     
{ 
	if(m_refCount != 1) 
		m_refCount = 0 ;   
	delete this; 
	return 0;  
}

NAMESPACE_LABOR3D_END